package com.edz.from0507to0513apk.Accountsave.aty;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;

import com.edz.from0507to0513apk.Accountsave.frag.MainFrag;
import com.edz.from0507to0513apk.Accountsave.frag.MsgFrag;
import com.edz.from0507to0513apk.Accountsave.frag.SettingFrag;
import com.edz.from0507to0513apk.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AccountSaveMainAty extends FragmentActivity {
    @BindView(R.id.navigation)
    BottomNavigationView navigationView;

    private MainFrag mainFragment;
    private MsgFrag msgFragment;
    private SettingFrag settingFragment;
    private int lastShowFragment = 0;
    private Fragment[] fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_save_main);
        ButterKnife.bind(this);
        //界面控件初始化
        initView();
        //显示Fragment初始化
        initFragment();
    }

    private void initView() {
        navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        if (lastShowFragment != 0) {
                            switchFrament(lastShowFragment, 0);
                            lastShowFragment = 0;
                        }
                        return true;
                    case R.id.navigation_dashboard:
                        if (lastShowFragment != 1) {
                            switchFrament(lastShowFragment, 1);
                            lastShowFragment = 1;
                        }
                        return true;
                    case R.id.navigation_notifications:
                        if (lastShowFragment != 2) {
                            switchFrament(lastShowFragment, 2);
                            lastShowFragment = 2;
                        }
                        return true;
                }
                return false;
            }
        });
    }

    private void switchFrament(int lastShowFragment, int i) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(fragments[lastShowFragment]);
        if (!fragments[i].isAdded()) {
            transaction.add(R.id.container, fragments[i]);
        }
        transaction.show(fragments[i]).commitAllowingStateLoss();
    }

    private void initFragment() {
        mainFragment = new MainFrag();
        msgFragment = new MsgFrag();
        settingFragment = new SettingFrag();
        fragments = new Fragment[]{mainFragment, msgFragment, settingFragment};
        lastShowFragment = 0;
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.container, mainFragment)
                .show(mainFragment)
                .commit();
    }
}
